/** Author: David Powell
This file produces Table 1 + Table 2 + Table A1 + Table A5 + Table C1
**/


clear all
set more off
set mat 800
set seed 8721

global dir "/jules/b/dpowell"
global DATA "${dir}/purdue/replication/DATA"
global OUTPUT "${dir}/purdue/replication/output"


use ${DATA}/finaldata if year>=1991


gen overdose_rate=100000*(overdose/totpop)
gen opioid_rate=100000*opioids/totpop
gen overdose_no_cocaine_rate=100000*(overdoses_no_cocaine/totpop)
gen opioid_no_cocaine_rate=100000*opioids_no_cocaine/totpop

***TABLE A1***
bys nontripl: sum xx* overdose_rate opioid_rate overdose_no_cocaine opioid_no_cocaine [w=totpop] if year<1996
bys nontripl: sum totpop if year<1996

foreach sss of numlist 6 16 17 36 48 {
	sum xx* overdose_rate opioid_rate overdose_no_cocaine opioid_no_cocaine [w=totpop] if year<1996 & stfips==`sss'
	sum totpop if year<1996 & stfips==`sss'
}
*************



tab stfips, gen(ss) nof
tab year, gen(ttt) nof


gen post1=nontripl*(year>=1996 & year<=2000)
gen post2=nontripl*(year>=2001 & year<=2010)
gen post3=nontripl*(year>=2011)

gen num=_n
local nnn=1
****Table 1, Cols 1 and 5****
foreach var of varlist overdose_rate opioid_rate  {
	gen beta`nnn'=.
	gen low`nnn'=.
	gen high`nnn'=.
	gen pv`nnn'=.
	reg `var' ss*  post* ttt*  , cluster(stf) 

	replace beta`nnn'=_b[post1] if num==2
	replace beta`nnn'=_b[post2] if num==3
	replace beta`nnn'=_b[post3] if num==4


qui {
	boottest post1, boottype(wild) weighttype(webb) reps(9999) seed(23857389)
	matrix A=r(CI)
	replace low`nnn'=A[1,1] if num==2
	replace high`nnn'=A[1,2] if num==2
	replace pv`nnn'=r(p) if num==2

	boottest post2, boottype(wild) weighttype(webb) reps(9999) seed(23857389)
	matrix A=r(CI)
	replace low`nnn'=A[1,1] if num==3
	replace high`nnn'=A[1,2] if num==3
	replace pv`nnn'=r(p) if num==3

	boottest post3, boottype(wild) weighttype(webb) reps(9999) seed(23857389)
	matrix A=r(CI)
	replace low`nnn'=A[1,1] if num==4
	replace high`nnn'=A[1,2] if num==4
	replace pv`nnn'=r(p) if num==4

}
	
	boottest (post1=0)(post2=0)(post3=0), boottype(wild) weighttype(webb) reps(9999) seed(23857389)
	local nnn=`nnn'+1
}


****Table 1, Cols 2 and 6****
foreach var of varlist overdose_rate opioid_rate  {
	gen beta`nnn'=.
	gen low`nnn'=.
	gen high`nnn'=.
	gen pv`nnn'=.
	reg `var' ss*  post* ttt* [aw=totpop]  , cluster(stf) 

	replace beta`nnn'=_b[post1] if num==2
	replace beta`nnn'=_b[post2] if num==3
	replace beta`nnn'=_b[post3] if num==4


qui {
	boottest post1, boottype(wild) weighttype(webb) reps(9999) seed(23857389)
	matrix A=r(CI)
	replace low`nnn'=A[1,1] if num==2
	replace high`nnn'=A[1,2] if num==2
	replace pv`nnn'=r(p) if num==2

	boottest post2, boottype(wild) weighttype(webb) reps(9999) seed(23857389)
	matrix A=r(CI)
	replace low`nnn'=A[1,1] if num==3
	replace high`nnn'=A[1,2] if num==3
	replace pv`nnn'=r(p) if num==3

	boottest post3, boottype(wild) weighttype(webb) reps(9999) seed(23857389)
	matrix A=r(CI)
	replace low`nnn'=A[1,1] if num==4
	replace high`nnn'=A[1,2] if num==4
	replace pv`nnn'=r(p) if num==4

}
	
	boottest (post1=0)(post2=0)(post3=0), boottype(wild) weighttype(webb) reps(9999) seed(23857389)
	local nnn=`nnn'+1
}




****Table 1, Cols 3 and 7****
foreach var of varlist overdose_rate opioid_rate  {
	gen beta`nnn'=.
	gen low`nnn'=.
	gen high`nnn'=.
	gen pv`nnn'=.
	reg `var' ss*  post* ttt* xx* [aw=totpop]  , cluster(stf) 

	replace beta`nnn'=_b[post1] if num==2
	replace beta`nnn'=_b[post2] if num==3
	replace beta`nnn'=_b[post3] if num==4


qui {
	boottest post1, boottype(wild) weighttype(webb) reps(9999) seed(23857389)
	matrix A=r(CI)
	replace low`nnn'=A[1,1] if num==2
	replace high`nnn'=A[1,2] if num==2
	replace pv`nnn'=r(p) if num==2

	boottest post2, boottype(wild) weighttype(webb) reps(9999) seed(23857389)
	matrix A=r(CI)
	replace low`nnn'=A[1,1] if num==3
	replace high`nnn'=A[1,2] if num==3
	replace pv`nnn'=r(p) if num==3

	boottest post3, boottype(wild) weighttype(webb) reps(9999) seed(23857389)
	matrix A=r(CI)
	replace low`nnn'=A[1,1] if num==4
	replace high`nnn'=A[1,2] if num==4
	replace pv`nnn'=r(p) if num==4

}
	
	boottest (post1=0)(post2=0)(post3=0), boottype(wild) weighttype(webb) reps(9999) seed(23857389)
	local nnn=`nnn'+1
}


drop ttt*
egen ry=group(region year)
tab ry, gen(ttt) nof



****Table 1, Cols 4 and 8****
foreach var of varlist overdose_rate opioid_rate  {
	gen beta`nnn'=.
	gen low`nnn'=.
	gen high`nnn'=.
	gen pv`nnn'=.
	reg `var' ss*  post* ttt* xx* [aw=totpop]  , cluster(stf) 

	replace beta`nnn'=_b[post1] if num==2
	replace beta`nnn'=_b[post2] if num==3
	replace beta`nnn'=_b[post3] if num==4


qui {
	boottest post1, boottype(wild) weighttype(webb) reps(9999) seed(23857389)
	matrix A=r(CI)
	replace low`nnn'=A[1,1] if num==2
	replace high`nnn'=A[1,2] if num==2
	replace pv`nnn'=r(p) if num==2

	boottest post2, boottype(wild) weighttype(webb) reps(9999) seed(23857389)
	matrix A=r(CI)
	replace low`nnn'=A[1,1] if num==3
	replace high`nnn'=A[1,2] if num==3
	replace pv`nnn'=r(p) if num==3

	boottest post3, boottype(wild) weighttype(webb) reps(9999) seed(23857389)
	matrix A=r(CI)
	replace low`nnn'=A[1,1] if num==4
	replace high`nnn'=A[1,2] if num==4
	replace pv`nnn'=r(p) if num==4

}
	
	boottest (post1=0)(post2=0)(post3=0), boottype(wild) weighttype(webb) reps(9999) seed(23857389)
	local nnn=`nnn'+1
}

drop ttt* ry
tab year, gen(ttt) nof

****Table 2, Col 2 (and Table A5, Col 2)****
***Large States****
foreach var of varlist overdose_rate opioid_rate  {
	gen beta`nnn'=.
	gen low`nnn'=.
	gen high`nnn'=.
	gen pv`nnn'=.
	reg `var' ss*  post* ttt* xx* [aw=totpop]  if  stf==17 | stf==36 | stf==48 | stf==6 | stf==12 | stf==42 | stf==39 | stf==26, cluster(stf) 

	replace beta`nnn'=_b[post1] if num==2
	replace beta`nnn'=_b[post2] if num==3
	replace beta`nnn'=_b[post3] if num==4


qui {
	boottest post1, boottype(wild) weighttype(webb) reps(9999) seed(23857389)
	matrix A=r(CI)
	replace low`nnn'=A[1,1] if num==2
	replace high`nnn'=A[1,2] if num==2
	replace pv`nnn'=r(p) if num==2

	boottest post2, boottype(wild) weighttype(webb) reps(9999) seed(23857389)
	matrix A=r(CI)
	replace low`nnn'=A[1,1] if num==3
	replace high`nnn'=A[1,2] if num==3
	replace pv`nnn'=r(p) if num==3

	boottest post3, boottype(wild) weighttype(webb) reps(9999) seed(23857389)
	matrix A=r(CI)
	replace low`nnn'=A[1,1] if num==4
	replace high`nnn'=A[1,2] if num==4
	replace pv`nnn'=r(p) if num==4

}
	
	boottest (post1=0)(post2=0)(post3=0), boottype(wild) weighttype(webb) reps(9999) seed(23857389)
	local nnn=`nnn'+1
}

****Table 2, Col 3 (and Table A5, Col 3)****
***States with PDMPs in 1996****
foreach var of varlist overdose_rate opioid_rate  {
	gen beta`nnn'=.
	gen low`nnn'=.
	gen high`nnn'=.
	gen pv`nnn'=.
	reg `var' ss*  post* ttt* xx* [aw=totpop] if nontripl==0 | stf==15 | stf==18 | stf==25 | stf==26 | stf==32 | stf==40 | stf==44 | stf==49 | stf==54 | stf==42, cluster(stf) 

	replace beta`nnn'=_b[post1] if num==2
	replace beta`nnn'=_b[post2] if num==3
	replace beta`nnn'=_b[post3] if num==4


qui {
	boottest post1, boottype(wild) weighttype(webb) reps(9999) seed(23857389)
	matrix A=r(CI)
	replace low`nnn'=A[1,1] if num==2
	replace high`nnn'=A[1,2] if num==2
	replace pv`nnn'=r(p) if num==2

	boottest post2, boottype(wild) weighttype(webb) reps(9999) seed(23857389)
	matrix A=r(CI)
	replace low`nnn'=A[1,1] if num==3
	replace high`nnn'=A[1,2] if num==3
	replace pv`nnn'=r(p) if num==3

	boottest post3, boottype(wild) weighttype(webb) reps(9999) seed(23857389)
	matrix A=r(CI)
	replace low`nnn'=A[1,1] if num==4
	replace high`nnn'=A[1,2] if num==4
	replace pv`nnn'=r(p) if num==4

}
	
	boottest (post1=0)(post2=0)(post3=0), boottype(wild) weighttype(webb) reps(9999) seed(23857389)
	local nnn=`nnn'+1
}


****Table 2, Col 4 (and Table A5, Col 4)****
***add policy controls****

foreach var of varlist overdose_rate opioid_rate  {
	gen beta`nnn'=.
	gen low`nnn'=.
	gen high`nnn'=.
	gen pv`nnn'=.
	reg `var' ss*  post* ttt* xx* pp* Any_PMP electronic Must [aw=totpop], cluster(stf) 

	replace beta`nnn'=_b[post1] if num==2
	replace beta`nnn'=_b[post2] if num==3
	replace beta`nnn'=_b[post3] if num==4


qui {
	boottest post1, boottype(wild) weighttype(webb) reps(9999) seed(23857389)
	matrix A=r(CI)
	replace low`nnn'=A[1,1] if num==2
	replace high`nnn'=A[1,2] if num==2
	replace pv`nnn'=r(p) if num==2

	boottest post2, boottype(wild) weighttype(webb) reps(9999) seed(23857389)
	matrix A=r(CI)
	replace low`nnn'=A[1,1] if num==3
	replace high`nnn'=A[1,2] if num==3
	replace pv`nnn'=r(p) if num==3

	boottest post3, boottype(wild) weighttype(webb) reps(9999) seed(23857389)
	matrix A=r(CI)
	replace low`nnn'=A[1,1] if num==4
	replace high`nnn'=A[1,2] if num==4
	replace pv`nnn'=r(p) if num==4

}
	
	boottest (post1=0)(post2=0)(post3=0), boottype(wild) weighttype(webb) reps(9999) seed(23857389)
	local nnn=`nnn'+1
}

***MAKE TABLE 1
preserve
keep beta* low* high* pv* 
keep if beta1<.

gen num=_n
expand 2
bysort num: gen c=_n

foreach nnn of numlist 1/8 {

	gen tmp=low`nnn'
	*format %12.3f tmp
	*gen str5 temp=string(tmp)
	gen str temp=string(tmp, "%12.3f")
	replace tmp=high`nnn'
	gen str temp2=string(tmp, "%12.3f")
	gen str ci`nnn' = "[" + temp + ", " + temp2 + "]"
	drop t*mp*

	gen result`nnn'=ci`nnn' if c==2
	gen str temp2=string(beta`nnn', "%12.3f")
	replace result`nnn'=temp2 if c==1 & pv`nnn'>.1
	replace result`nnn'=temp2 + "*" if c==1 & pv`nnn'<=.1
	replace result`nnn'=temp2 + "**" if c==1 & pv`nnn'<=.05
	replace result`nnn'=temp2 + "***" if c==1 & pv`nnn'<=.01
	drop temp*
}

keep result*

order result1 result3 result5 result7 result2 result4 result6 result8
list

restore


***MAKE TABLE 2 and Table A5
preserve
keep beta* low* high* pv* 
keep if beta1<.

gen num=_n
expand 2
bysort num: gen c=_n

foreach nnn of numlist 5/6 9/14 {

	gen tmp=low`nnn'
	*format %12.3f tmp
	*gen str5 temp=string(tmp)
	gen str temp=string(tmp, "%12.3f")
	replace tmp=high`nnn'
	gen str temp2=string(tmp, "%12.3f")
	gen str ci`nnn' = "[" + temp + ", " + temp2 + "]"
	drop t*mp*

	gen result`nnn'=ci`nnn' if c==2
	gen str temp2=string(beta`nnn', "%12.3f")
	replace result`nnn'=temp2 if c==1 & pv`nnn'>.1
	replace result`nnn'=temp2 + "*" if c==1 & pv`nnn'<=.1
	replace result`nnn'=temp2 + "**" if c==1 & pv`nnn'<=.05
	replace result`nnn'=temp2 + "***" if c==1 & pv`nnn'<=.01
	drop temp*
}

keep result*

order result5 result9 result11 result13 result6 result10 result12 result14
list

restore





****Table C1****
capture drop beta* low* high* pv*
local nnn=1
foreach var of varlist overdose_rate opioid_rate  {
	gen beta`nnn'=.
	gen low`nnn'=.
	gen high`nnn'=.
	gen pv`nnn'=.
	reg `var' ss*  post* ttt* xx* unemp [aw=totpop], cluster(stf) 

	replace beta`nnn'=_b[post1] if num==2
	replace beta`nnn'=_b[post2] if num==3
	replace beta`nnn'=_b[post3] if num==4


qui {
	boottest post1, boottype(wild) weighttype(webb) reps(9999) seed(23857389)
	matrix A=r(CI)
	replace low`nnn'=A[1,1] if num==2
	replace high`nnn'=A[1,2] if num==2
	replace pv`nnn'=r(p) if num==2

	boottest post2, boottype(wild) weighttype(webb) reps(9999) seed(23857389)
	matrix A=r(CI)
	replace low`nnn'=A[1,1] if num==3
	replace high`nnn'=A[1,2] if num==3
	replace pv`nnn'=r(p) if num==3

	boottest post3, boottype(wild) weighttype(webb) reps(9999) seed(23857389)
	matrix A=r(CI)
	replace low`nnn'=A[1,1] if num==4
	replace high`nnn'=A[1,2] if num==4
	replace pv`nnn'=r(p) if num==4

}
	
	boottest (post1=0)(post2=0)(post3=0), boottype(wild) weighttype(webb) reps(9999) seed(23857389)
	local nnn=`nnn'+1
}



foreach var of varlist overdose_rate opioid_rate  {
	gen beta`nnn'=.
	gen low`nnn'=.
	gen high`nnn'=.
	gen pv`nnn'=.
	reg `var' ss*  post* ttt* xx* bartik_t [aw=totpop], cluster(stf) 

	replace beta`nnn'=_b[post1] if num==2
	replace beta`nnn'=_b[post2] if num==3
	replace beta`nnn'=_b[post3] if num==4


qui {
	boottest post1, boottype(wild) weighttype(webb) reps(9999) seed(23857389)
	matrix A=r(CI)
	replace low`nnn'=A[1,1] if num==2
	replace high`nnn'=A[1,2] if num==2
	replace pv`nnn'=r(p) if num==2

	boottest post2, boottype(wild) weighttype(webb) reps(9999) seed(23857389)
	matrix A=r(CI)
	replace low`nnn'=A[1,1] if num==3
	replace high`nnn'=A[1,2] if num==3
	replace pv`nnn'=r(p) if num==3

	boottest post3, boottype(wild) weighttype(webb) reps(9999) seed(23857389)
	matrix A=r(CI)
	replace low`nnn'=A[1,1] if num==4
	replace high`nnn'=A[1,2] if num==4
	replace pv`nnn'=r(p) if num==4

}
	
	boottest (post1=0)(post2=0)(post3=0), boottype(wild) weighttype(webb) reps(9999) seed(23857389)
	local nnn=`nnn'+1
}

foreach var of varlist overdose_rate opioid_rate  {
	gen beta`nnn'=.
	gen low`nnn'=.
	gen high`nnn'=.
	gen pv`nnn'=.
	reg `var' ss*  post* ttt* xx* bartik* [aw=totpop], cluster(stf) 

	replace beta`nnn'=_b[post1] if num==2
	replace beta`nnn'=_b[post2] if num==3
	replace beta`nnn'=_b[post3] if num==4


qui {
	boottest post1, boottype(wild) weighttype(webb) reps(9999) seed(23857389)
	matrix A=r(CI)
	replace low`nnn'=A[1,1] if num==2
	replace high`nnn'=A[1,2] if num==2
	replace pv`nnn'=r(p) if num==2

	boottest post2, boottype(wild) weighttype(webb) reps(9999) seed(23857389)
	matrix A=r(CI)
	replace low`nnn'=A[1,1] if num==3
	replace high`nnn'=A[1,2] if num==3
	replace pv`nnn'=r(p) if num==3

	boottest post3, boottype(wild) weighttype(webb) reps(9999) seed(23857389)
	matrix A=r(CI)
	replace low`nnn'=A[1,1] if num==4
	replace high`nnn'=A[1,2] if num==4
	replace pv`nnn'=r(p) if num==4

}
	
	boottest (post1=0)(post2=0)(post3=0), boottype(wild) weighttype(webb) reps(9999) seed(23857389)
	local nnn=`nnn'+1
}

foreach yyy of numlist 1983/1994 1996/2017 {
	foreach vvv of varlist NTR  {
		gen bbb`vvv'_`yyy'=`vvv'*(year==`yyy')
	}
}

foreach var of varlist overdose_rate opioid_rate  {
	gen beta`nnn'=.
	gen low`nnn'=.
	gen high`nnn'=.
	gen pv`nnn'=.
	reg `var' ss*  post* ttt* xx* bartik* bbb* [aw=totpop], cluster(stf) 

	replace beta`nnn'=_b[post1] if num==2
	replace beta`nnn'=_b[post2] if num==3
	replace beta`nnn'=_b[post3] if num==4


qui {
	boottest post1, boottype(wild) weighttype(webb) reps(9999) seed(23857389)
	matrix A=r(CI)
	replace low`nnn'=A[1,1] if num==2
	replace high`nnn'=A[1,2] if num==2
	replace pv`nnn'=r(p) if num==2

	boottest post2, boottype(wild) weighttype(webb) reps(9999) seed(23857389)
	matrix A=r(CI)
	replace low`nnn'=A[1,1] if num==3
	replace high`nnn'=A[1,2] if num==3
	replace pv`nnn'=r(p) if num==3

	boottest post3, boottype(wild) weighttype(webb) reps(9999) seed(23857389)
	matrix A=r(CI)
	replace low`nnn'=A[1,1] if num==4
	replace high`nnn'=A[1,2] if num==4
	replace pv`nnn'=r(p) if num==4

}
	
	boottest (post1=0)(post2=0)(post3=0), boottype(wild) weighttype(webb) reps(9999) seed(23857389)
	local nnn=`nnn'+1
}


***MAKE TABLE C1
preserve
keep beta* low* high* pv* 
keep if beta1<.

gen num=_n
expand 2
bysort num: gen c=_n

foreach nnn of numlist 1/8 {

	gen tmp=low`nnn'
	*format %12.3f tmp
	*gen str5 temp=string(tmp)
	gen str temp=string(tmp, "%12.3f")
	replace tmp=high`nnn'
	gen str temp2=string(tmp, "%12.3f")
	gen str ci`nnn' = "[" + temp + ", " + temp2 + "]"
	drop t*mp*

	gen result`nnn'=ci`nnn' if c==2
	gen str temp2=string(beta`nnn', "%12.3f")
	replace result`nnn'=temp2 if c==1 & pv`nnn'>.1
	replace result`nnn'=temp2 + "*" if c==1 & pv`nnn'<=.1
	replace result`nnn'=temp2 + "**" if c==1 & pv`nnn'<=.05
	replace result`nnn'=temp2 + "***" if c==1 & pv`nnn'<=.01
	drop temp*
}

keep result*

order result1 result3 result5 result7 result2 result4 result6 result8
list

restore
